package cn.sxt.sys.realm;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import cn.sxt.sys.constast.SYS_Constast;
import cn.sxt.sys.domain.Permission;
import cn.sxt.sys.domain.Role;
import cn.sxt.sys.domain.User;
import cn.sxt.sys.service.PermissionService;
import cn.sxt.sys.service.RoleService;
import cn.sxt.sys.service.UserService;
import cn.sxt.sys.utils.ActivierUser;

public class UserRealm extends AuthorizingRealm {

	@Autowired
	private UserService userService;
	
	@Autowired
	private PermissionService permissionService;
	
	@Autowired
	private RoleService roleService;
	
	// 认证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//得到身份
		String loginname=token.getPrincipal().toString();
		//根据身份查用户
		User user = userService.queryUserByLoginName(loginname);
		if(null !=user) {
			String dbPwd = user.getPwd(); //数据库加密的密码
			ActivierUser activierUser=new ActivierUser();
			activierUser.setCurUser(user);
			//根据用户id查角色
			List<Role> roleList=roleService.queryRoleByUserId(user.getId());
			List<String> roles=new ArrayList<>();
			for (Role r : roleList) {
				roles.add(r.getName());
			}
			//根据id查权限
			List<Permission> permissionList=permissionService.queryPermissionByUserIdForList(SYS_Constast.PERMISSION_TYPE_PERMISSION,user.getId());
			List<String> permissions=new ArrayList<>();
			for (Permission p : permissionList) {
				permissions.add(p.getPercode());
			}
			//发到activerUser
			activierUser.setRoles(roles);
			activierUser.setPermissions(permissions);
			
			ByteSource credentialsSalt=ByteSource.Util.bytes(user.getSalt());//盐
			AuthenticationInfo info=new SimpleAuthenticationInfo(activierUser, dbPwd, credentialsSalt, getName());
			return info;
		}else {
			return null;
		}
	}

	//授权
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
		//得到activerUSer
		ActivierUser activierUser=(ActivierUser) principals.getPrimaryPrincipal();
		if(activierUser.getCurUser().getType()==SYS_Constast.USER_TYPE_SUPER) {
			//判断，超管所有按钮可看
			info.addRole("*");
			info.addStringPermission("*:*");
		}else {
			if(activierUser.getRoles().size()>0) {
				info.addRoles(activierUser.getRoles());
			}
			if(activierUser.getPermissions().size()>0) {
				info.addStringPermissions(activierUser.getPermissions());
			}
		}
		return info;
	}

}
